<?php

/**
 * @property string $file_id
 * @property string $client_id
 * @property string $file_name
 * @property string $file_mime
 * @property string $file_size
 * @property string $file_path
 * @property integer $store_type
 * @property string $oss_key
 * @property string $oss_bucket
 * @property integer $refer_type
 * @property string $create_time
 *
 * @author ben <ben@spicatech.cn>  Created on 2015-8-17
 */
class UploadFile extends MainActiveRecord {

    const STORE_TYPE_LOCAL = 1;
    const STORE_TYPE_ALIYUN = 2;
    
    
    const REFER_TYPE_COURSE = 20;
    
	const ALIYUN_URL = 'https://wc-rent.oss-cn-hangzhou.aliyuncs.com';
    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'tbl_upload_file';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('store_type, refer_type', 'numerical', 'integerOnly' => true),
            array('file_name, file_path, oss_key, oss_bucket', 'length', 'max' => 100),
            array('file_mime', 'length', 'max' => 50),
            array('client_id', 'length', 'max' => 10),
            array('file_size', 'length', 'max' => 20),
            array('create_time', 'safe'),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('file_id, file_name, file_mime, file_size, file_path, store_type, oss_key, oss_bucket, refer_type, create_time', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'file_id' => 'File',
            'file_name' => 'File Name',
            'file_mime' => 'File Mime',
            'file_size' => 'File Size',
            'file_path' => 'File Path',
            'store_type' => 'Store Type',
            'oss_key' => 'Oss Key',
            'oss_bucket' => 'Oss Bucket',
            'refer_type' => 'Refer Type',
            'create_time' => 'Create Time',
        );
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return UploadFile the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }
    
    public static function getAllUrl(array $ids){
    	foreach ($ids as $v){
    		if(!$v){
    			return false; 
    		}
    		$img = self::model()->findById($v);
    		if($img->store_type == self::STORE_TYPE_ALIYUN){
    			$list[] = self::ALIYUN_URL . $img->file_name; 
    		}else{
    			$list[] = 'http://res.wecheng.com.cn/'.$img->file_path;
    		}
    		 
    	}
    	
    	return $list ; 
    }
    
    public function getUrl() {
        if($this->store_type == self::STORE_TYPE_LOCAL) {
            return 'http://res.wecheng.com.cn'.$this->file_path;
        }
        return '';
    }
    
    /**
     * 
     * @param type $id
     * @return UploadFile
     */
    public static function findById($id) {
        return self::model()->findByPk($id);
    }

}
